//
//  WriteLetterView.m
//  DirectProDuct
//
//  Created by hl on 16/1/22.
//  Copyright (c) 2016年 ping. All rights reserved.
//

#import "WriteLetterView.h"
#define Font 12.0f
@implementation WriteLetterView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
 
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
        [self createdSubView];
        
    }
    return self;
}
-(void)createdSubView{
    backScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    backScrollView.scrollEnabled = YES;
    backScrollView.contentSize = CGSizeMake(0, KScreenHeight -78);
    [self addSubview:backScrollView];
    
    self.sendPeopleField = [self createdTextFieldWithIndex:0 andWithLabelTitle:@"发件人:"];
    self.receivePeopleField = [self createdTextFieldWithIndex:1 andWithLabelTitle:@"收件人:"];
    self.mainField = [self createdTextFieldWithIndex:2 andWithLabelTitle:@"主 题:"];
    
    self.mainTextView = [[UITextView alloc] initWithFrame:CGRectMake(self.sendPeopleField.left, self.mainField.bottom + 10 * _kscH, self.mainField.width, KScreenHeight - 78 - self.mainField.bottom - 10 * _kscH - 55 * _kscH)];
    self.mainTextView.layer.cornerRadius = 5;
    self.mainTextView.clipsToBounds = YES;
    self.mainTextView.layer.borderWidth = 0.8 * _kscH;
    self.mainTextView.layer.borderColor = XiaHuaXianCOLOR.CGColor;
    self.mainTextView.delegate = self;
    self.mainTextView.returnKeyType = UIReturnKeyDone;
    [backScrollView addSubview:self.mainTextView];
    
    self.sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(90 * _kscW, self.mainTextView.bottom + 10 * _kscH, 57 * _kscW, 30 * _kscH)];
    [self.sendBtn setBackgroundColor:[UIColor orangeColor]];
    self.sendBtn.tag = 0;
    self.sendBtn.layer.cornerRadius = 3;
    self.sendBtn.clipsToBounds = YES;
    [self.sendBtn addTarget:self.delegate action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.sendBtn setTitle:@"发 送" forState:UIControlStateNormal];
    self.sendBtn.titleLabel.font = [UIFont fontWithName:TextJiaCu size:15.0];
    [backScrollView addSubview:self.sendBtn];
    
    self.caoGaoOrDeleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.sendBtn.right+23 *_kscW, self.sendBtn.top, self.sendBtn.width, self.sendBtn.height)];
    [self.caoGaoOrDeleteBtn setBackgroundColor:RGBACOLOR(1, 183, 118, 1)];
    self.caoGaoOrDeleteBtn.tag = 1;
    self.caoGaoOrDeleteBtn.layer.cornerRadius = 3;
    self.caoGaoOrDeleteBtn.clipsToBounds = YES;
    [self.caoGaoOrDeleteBtn addTarget:self.delegate action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.caoGaoOrDeleteBtn setTitle:@"存草稿" forState:UIControlStateNormal];
    self.caoGaoOrDeleteBtn.titleLabel.font = [UIFont fontWithName:TextJiaCu size:15.0];
    [backScrollView addSubview:self.caoGaoOrDeleteBtn];

}
-(UITextField *)createdTextFieldWithIndex:(int)index andWithLabelTitle:(NSString *)title{
    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(15 * _kscW, 20 * _kscH + 35 * _kscH*index, KScreenWidth - 30 * _kscW, 25 * _kscH)];
    field.font = [UIFont systemFontOfSize:Font];
    
    field.delegate = self;
    field.returnKeyType = UIReturnKeyDone;
    UILabel *leftViewLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50 * _kscW, field.height)];
    leftViewLab.font = [UIFont systemFontOfSize:Font];
    field.leftView = leftViewLab;
    field.leftViewMode = UITextFieldViewModeAlways;
    leftViewLab.text = title;
    [backScrollView addSubview:field];
    
    UILabel *lineLab = [[UILabel alloc] initWithFrame:CGRectMake(field.left, field.bottom + 3*_kscH, field.width, 1 * _kscH)];
    lineLab.backgroundColor = XiaHuaXianCOLOR;
    [backScrollView addSubview:lineLab];
    
    return field;
    
    
}
-(void)keyboardWillShow:(id)aNotification{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    backScrollView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - 78 - keyboardRect.size.height);
    
}
-(void)keyboardWillHide:(id)sender{
    backScrollView.frame = self.bounds;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
